Python for Bioinformatics

This Jupyter notebook is intented to be used alongside the book Python for Bioinformatics

Chapter 18: Calculating Melting Temperature from a Set of Primers

Note: Before opening the file, this file should be accesible from this Jupyter notebook. In order to do so, the following commands will download these files from Github and extract them into a directory called samples.


In [0]:
!pip install biopython

In [7]:
!curl https://raw.githubusercontent.com/Serulab/Py4Bio/master/samples/samples.tar.bz2 -o samples.tar.bz2
!mkdir samples
!tar xvfj samples.tar.bz2 -C samples


  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 16.5M  100 16.5M    0     0  22.3M      0 --:--:-- --:--:-- --:--:-- 22.3M
mkdir: cannot create directory ‘samples’: File exists
._.
./
./vectorssmall.fasta
./prot.fas
./conglycinin.phy
./input4align.dnd
./Q5R5X8.fas
./Q9JJE1.xml
./primers.txt
./NC_006581.gb
./PythonU.db
./hsc1.fasta
./B1.csv
./sampleXblast.xml
./sampledata.xlsx
./NC2033.txt
./conglycinin.dnd
./BLAST_output.xml
./UniVec_Core.nhr
./seqA.fas
./fishdata.csv
./template
./pdbaa
./bioinfo/
./fishbacteria.csv
./B1IXL9.txt
./._GSM188012.CEL
./GSM188012.CEL
./example.aln
./data.csv
./3seqs.fas
./BcrA.gp
./uniprotrecord.xml
./contig1.ace
./other.xml
./UniVec_Core.nsq
./test3.csv
./conglycinin.multiple.phy
./fasta22.fas
./pMOSBlue.txt
./readme.txt
./sampleX.fas
./UniVec_Core.nin
./pdb1apk.ent.gz
./cas9align.fasta
./a19.gp
./phd1
./t3beta.fasta
./conglycinin.fasta
./UniVec_Core
./BLAST_output.html
./spfile.txt
./input4align.fasta
./t3.fasta
./TAIR7_Transcripts_by_map_position.gz
./bioinfo/seqs/
./bioinfo/seqs/513710.fasta
./bioinfo/seqs/6598312.fasta
./bioinfo/seqs/4586830.fasta
./bioinfo/seqs/15721870.fasta
./bioinfo/seqs/2623545.fasta
./bioinfo/seqs/218744616.fasta
./bioinfo/seqs/7415878.fasta
./bioinfo/seqs/63108399.fasta
./bioinfo/seqs/513717.fasta
./bioinfo/seqs/7638455.fasta
./bioinfo/seqs/513719.fasta
./bioinfo/seqs/513419.fasta
./bioinfo/seqs/513718.fasta

Listing 18.1: fromtxt.py: Primer Tm calculation


In [8]:
from Bio.SeqUtils import MeltingTemp as MT

PRIMER_FILE = 'samples/primers.txt'
for line in open(PRIMER_FILE):
    # prm stores the primer, without 5'- and -3'
    prm = line[3:len(line)-4].replace(' ','')
    # .2f is used to print up to 2 decimals.
    print('{0},{1:.2f}'.format(prm, MT.Tm_staluc(prm)))


TCTAGAACTAGTGGATC,39.49
GTTTTCCCAGTCACGACG,50.57
CGATTTAGGTGACACTATAG,44.66
CAGGAAACAGCTATGACC,46.73
GTGTGGAATTGTGAGCGG,50.87
TGTAAAACGACGGCCAGT,51.14
CGAGGTCGACGGTATCG,51.20
TAATACGACTCACTATAGGG,44.59
GCGCAATTAACCCTCACTAAAG,52.25

Listing 18.2: toexcel.py: Primer Tm calculation, Excel output


In [0]:
from Bio.SeqUtils import MeltingTemp as MT
import xlwt

PRIMER_FILE = 'samples/primers.txt'
# w is the name of a newly created workbook.
w = xlwt.Workbook()
# ws is the name of a new sheet in this workbook.
ws = w.add_sheet('Result')
# These two lines writes the titles of the columns.
ws.write(0, 0, 'Primer Sequence')
ws.write(0, 1, 'Tm')
for index, line in enumerate(open(PRIMER_FILE)):
    # For each line in the input file, write the primer
    # sequence and the Tm
    prm = line[3:len(line)-4].replace(' ','')
    ws.write(index+1, 0, prm)
    ws.write(index+1, 1, '{0:.2f}'.format(MT.Tm_staluc(prm)))
# Save the spreadsheel into a file.
w.save('primerout.xls')

In [0]: